/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * LiberAcus
 * Copyright (C) 2012 - 2014 Massimiliano Maniscalco <max.maniscalco@gmail.com>
 * 
LiberAcus is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * LiberAcus is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _PARSER_XML_H_
#define _PARSER_XML_H_

#include <gtkmm.h>
#include <libxml++/libxml++.h>
#include "cliente.h"
#include "prestazione.h"
#include "capo.h"
#include "ricevuta.h"
#include "preferenze.h"

#define FILE_TIPO_SCONOSCIUTO	0
#define FILE_TIPO_DATA					1
#define FILE_TIPO_PREF					2

// Preferenze
#define PREF_INTESTAZIONE			-1
#define PREF_SALUTO					-2
#define PREF_ELIMINAZIONE			-3
#define PREF_AZZERAMENTO			-4
#define PREF_SAVE_USCITA			-5
#define PREF_STAMPA_NR				-6
#define PREF_VISUAL_NR				-7

// classi
#define NESSUNA_CLASSE				0
#define CLASSE_PRESTAZIONE		1
#define CLASSE_RICEVUTA				2
#define CLASSE_RICEVUTA_V2		3
#define CLASSE_CLIENTE				4

// Prestazione
#define PREST_CAPO						11
#define PREST_DESCRIZIONE			12
#define PREST_PREZZO					13
#define PREST_QUANTITA				14
#define PREST_COMPLETATA			15

// Ricevuta
#define RICEV_NUMERO					21
#define RICEV_NOME						22
#define RICEV_DATA_ACC				23
#define RICEV_ORA_ACC				24
#define RICEV_ACCONTO				25
#define RICEV_DATA_RIT					26
#define RICEV_ORA_RIT					27
#define RICEV_URGENTE				28
#define RICEV_CONSEGNATO			29

// Capo
#define CAPO_NUMERO					31
#define CAPO_NOME						32

// Cliente
#define	CLIEN_NOME						41
#define CLIEN_TELEFONO				42

using namespace std;

class ParserXML: public xmlpp::SaxParser 
{
	
public:
	inline ParserXML() :
		file_tipo ( 0 ), codice_classe ( 0 ), codice_elemento ( 0 ), indice ( 0 ), clnt ( new Cliente() ), prest ( new Prestazione() ), capo ( new Capo() ), ricev ( new Ricevuta() ), preferenze ( Preferenze() )
	{ }
	virtual inline ~ParserXML();

	typedef sigc::signal<void, Preferenze> Signal_preferenze_loaded;
	typedef sigc::signal<void, time_t> Signal_date_file;
	typedef sigc::signal<void, Cliente> Signal_cliente_imported;
	typedef sigc::signal<void, Prestazione> Signal_prestazione_imported;
	typedef sigc::signal<void, Ricevuta> Signal_ricevuta_imported;
	typedef sigc::signal<void> Signal_importing_end;

	inline Signal_preferenze_loaded signal_preferenze_loaded ( void ) { return commit_load_preferenze; }
	inline Signal_date_file signal_date_file ( void ) { return commit_date_file; }
	inline Signal_cliente_imported signal_cliente_imported ( void ) { return commit_import_cliente; }
	inline Signal_prestazione_imported signal_prestazione_imported ( void ) { return commit_import_prestazione; }
	inline Signal_ricevuta_imported signal_ricevuta_imported ( void ) { return commit_import_ricevuta; }	
	inline Signal_importing_end signal_importing_end ( void ) { return commit_importing_end; }

protected:
#ifdef LIBXMLCPP_EXCEPTIONS_ENABLED
	virtual void on_start_document();
	virtual void on_end_document();
	virtual void on_start_element(const Glib::ustring& name, const AttributeList& properties);
	virtual void on_end_element(const Glib::ustring& name);
	virtual void on_characters(const Glib::ustring& characters);
	virtual void on_comment(const Glib::ustring& text);
	virtual void on_warning(const Glib::ustring& text);
	virtual void on_error(const Glib::ustring& text);
	virtual void on_fatal_error(const Glib::ustring& text);
#endif

private:
	int codice_elemento;
	char codice_classe;
	char file_tipo;
	unsigned short indice;
	struct tm data_ora;
	time_t data_salvataggio;
	
	Signal_preferenze_loaded commit_load_preferenze;
	Signal_date_file commit_date_file;
	Signal_cliente_imported commit_import_cliente;
	Signal_prestazione_imported commit_import_prestazione;
	Signal_ricevuta_imported commit_import_ricevuta;
	Signal_importing_end commit_importing_end;
	
	Prestazione *prest;
	Capo *capo;
	Ricevuta *ricev;
	Cliente *clnt;
	Preferenze preferenze;
	
};

inline ParserXML::
~ParserXML()
{
	if ( clnt )
		delete clnt;
	if ( prest )
		delete prest;
	if ( capo )
		delete capo;
	if ( ricev )
		delete ricev;
}

#endif // _PARSER_XML_H_
